package de.openlaboratory.cmis.repository;

import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.FileableCmisObject;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Item;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.ObjectType;
import org.apache.chemistry.opencmis.client.api.OperationContext;
import org.apache.chemistry.opencmis.client.api.Policy;
import org.apache.chemistry.opencmis.client.api.Property;
import org.apache.chemistry.opencmis.client.api.Relationship;
import org.apache.chemistry.opencmis.client.api.Rendition;
import org.apache.chemistry.opencmis.client.api.SecondaryType;
import org.apache.chemistry.opencmis.client.api.TransientCmisObject;
import org.apache.chemistry.opencmis.client.api.TransientFolder;
import org.apache.chemistry.opencmis.client.api.Tree;
import org.apache.chemistry.opencmis.commons.data.Ace;
import org.apache.chemistry.opencmis.commons.data.Acl;
import org.apache.chemistry.opencmis.commons.data.AllowableActions;
import org.apache.chemistry.opencmis.commons.data.CmisExtensionElement;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
import org.apache.chemistry.opencmis.commons.enums.ExtensionLevel;
import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;

import de.openlaboratory.elib.core.LogManager;
import de.openlaboratory.elib.model.ImportFile;

public class LocalFolder implements Folder
{
	private String folderId;
	private Path folder;

	public LocalFolder(String rootFolderId)
	{
		LogManager.logMessage("Create folder: " + rootFolderId );
		
		this.folderId = rootFolderId;
		
		String folderPath = convertFolderIdToFolderPath(this.folderId);
		
		LogManager.logMessage("Retrieve Folder: " + folderPath + " (id: " + folderId + ")");
		
		this.folder = Paths.get(folderPath);
	}
	
	public static String convertFolderIdToFolderPath(String p)
	{
		return p;
	}

	@Override
	public void addToFolder(ObjectId arg0, boolean arg1)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public List<Folder> getParents()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Folder> getParents(OperationContext arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> getPaths()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FileableCmisObject move(ObjectId arg0, ObjectId arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FileableCmisObject move(ObjectId arg0, ObjectId arg1, OperationContext arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void removeFromFolder(ObjectId arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Acl addAcl(List<Ace> arg0, AclPropagation arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Acl applyAcl(List<Ace> arg0, List<Ace> arg1, AclPropagation arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void applyPolicy(ObjectId... arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void delete()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(boolean arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Acl getAcl()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T getAdapter(Class<T> arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AllowableActions getAllowableActions()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CmisExtensionElement> getExtensions(ExtensionLevel arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Policy> getPolicies()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getRefreshTimestamp()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Relationship> getRelationships()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Rendition> getRenditions()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Deprecated
	public TransientCmisObject getTransientObject()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void refresh()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void refreshIfOld(long arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Acl removeAcl(List<Ace> arg0, AclPropagation arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void removePolicy(ObjectId... arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public Acl setAcl(List<Ace> arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmisObject updateProperties(Map<String, ?> arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectId updateProperties(Map<String, ?> arg0, boolean arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getId()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ObjectType> findObjectType(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectType getBaseType()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseTypeId getBaseTypeId()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getChangeToken()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreatedBy()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GregorianCalendar getCreationDate()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDescription()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GregorianCalendar getLastModificationDate()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getLastModifiedBy()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Property<?>> getProperties()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> Property<T> getProperty(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T getPropertyValue(String arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SecondaryType> getSecondaryTypes()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ObjectType getType()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ObjectType> getAllowedChildObjectTypes()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getParentId()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Document createDocument(Map<String, ?> arg0, ContentStream arg1, VersioningState arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Document createDocument(Map<String, ?> arg0, ContentStream arg1, VersioningState arg2, List<Policy> arg3, List<Ace> arg4, List<Ace> arg5, OperationContext arg6)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Document createDocumentFromSource(ObjectId arg0, Map<String, ?> arg1, VersioningState arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Document createDocumentFromSource(ObjectId arg0, Map<String, ?> arg1, VersioningState arg2, List<Policy> arg3, List<Ace> arg4, List<Ace> arg5, OperationContext arg6)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Folder createFolder(Map<String, ?> arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Folder createFolder(Map<String, ?> arg0, List<Policy> arg1, List<Ace> arg2, List<Ace> arg3, OperationContext arg4)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Item createItem(Map<String, ?> arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Item createItem(Map<String, ?> arg0, List<Policy> arg1, List<Ace> arg2, List<Ace> arg3, OperationContext arg4)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Policy createPolicy(Map<String, ?> arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Policy createPolicy(Map<String, ?> arg0, List<Policy> arg1, List<Ace> arg2, List<Ace> arg3, OperationContext arg4)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> deleteTree(boolean arg0, UnfileObject arg1, boolean arg2)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<Document> getCheckedOutDocs()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<Document> getCheckedOutDocs(OperationContext arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ItemIterable<CmisObject> getChildren()
	{
		List<Path> files = new ArrayList<Path>();
		
		if (this.folder != null)
		{
			if (this.folder.isAbsolute())
			{
				try (DirectoryStream<Path> ds = Files.newDirectoryStream(this.folder))
				{
					for (Path p : ds)
					{
						files.add(p);
					}
	
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}

		ItemIterable<CmisObject> children = new LocalFileList<CmisObject>(files);
		
		return children;
	}

	@Override
	public ItemIterable<CmisObject> getChildren(OperationContext arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Tree<FileableCmisObject>> getDescendants(int arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Tree<FileableCmisObject>> getDescendants(int arg0, OperationContext arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Folder getFolderParent()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Tree<FileableCmisObject>> getFolderTree(int arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Tree<FileableCmisObject>> getFolderTree(int arg0, OperationContext arg1)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPath()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@Deprecated
	public TransientFolder getTransientFolder()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isRootFolder()
	{
		// TODO Auto-generated method stub
		return false;
	}

}
